package com.uzai.console.service.report.impl;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.uzai.common.dto.device.DeviceInfoWxpubCacheDto;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.enums.TakeBalStatusEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.common.web.interceptor.TraceIdInterceptor;
import com.uzai.console.constant.CacheKey;
import com.uzai.console.dto.feign.report.RealTimeOverviewReportFeignDto;
import com.uzai.console.dto.report.RealTimeOverviewReportDto;
import com.uzai.console.dto.takebal.usertakebal.UserTakeBalQuery;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatInfoQuery;
import com.uzai.console.dto.wechatwork.devicewxworkinfo.DeviceWxworkInfoQuery;
import com.uzai.console.dto.wxpubmanage.WxpubInfoListQuery;
import com.uzai.console.entity.DeviceWechatInfo;
import com.uzai.console.entity.DeviceWxworkInfo;
import com.uzai.console.entity.wxpubmanage.WxpubInfoEntity;
import com.uzai.console.mapper.DeviceWechatInfoMapper;
import com.uzai.console.mapper.DeviceWxworkInfoMapper;
import com.uzai.console.mapper.UserTakeBalMapper;
import com.uzai.console.mapper.WxpubInfoMapper;
import com.uzai.common.utils.Math;
import com.uzai.console.mapper.stats.OrderProfitStatsMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.feign.UzaiReportServerFeignService;
import com.uzai.console.service.report.RealTimeOverviewReportService;
import com.uzai.console.vo.feign.report.RealTimeOverviewReportFeignVo;
import com.uzai.console.vo.report.OrderRevenueReportVo;
import com.uzai.console.vo.report.RealTimeOverviewReportVo;
import com.uzai.console.vo.report.RealTimeOverviewResultVo;
import com.uzai.console.vo.takebal.usertakebal.UserTakeBalReportVo;
import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 实时总览报表
 * @author liuqi
 * @date 2021年08月25日 20:18
 */
@Service
public class RealTimeOverviewReportServiceImpl implements RealTimeOverviewReportService {

    private Logger logger = LoggerFactory.getLogger(RealTimeOverviewReportServiceImpl.class);

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OrderProfitStatsMapper orderProfitStatsMapper;
    @Autowired
    private DeviceWechatInfoMapper deviceWechatInfoMapper;
    @Autowired
    private DeviceWxworkInfoMapper deviceWxworkInfoMapper;
    @Autowired
    private WxpubInfoMapper wxpubInfoMapper;
    @Autowired
    private UzaiReportServerFeignService uzaiReportServerFeignService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private UserTakeBalMapper userTakeBalMapper;

    /**
     * 实时总览报表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param realTimeOverviewReportDto
     * @return null
     */
    public RealTimeOverviewResultVo realTimeOverviewReport(RealTimeOverviewReportDto realTimeOverviewReportDto){

        //返回到页面结果
        RealTimeOverviewResultVo realTimeOverviewResultVo = new RealTimeOverviewResultVo();
        //查询开始时间
        Integer queryStartTime = realTimeOverviewReportDto.getQueryStartTime();
        //查询结束时间
        Integer queryEndTime = realTimeOverviewReportDto.getQueryEndTime();

        //查询时间范围为空，则查询今天、昨天、本月的数据
        if(queryStartTime == null || queryEndTime == null){
            //查询今天的实时总览
            //今天
            String today = Tools.getDateMonthDayStrTwo();
            //今天的开始时间戳
            Integer todayStart = DateUtil.getStrTime(today + " 00:00:00");
            //今天的结束时间错
            Integer todayEnd = DateUtil.getStrTime(today + " 23:59:59");
            //今天的参数对象
            RealTimeOverviewReportDto realTimeOverviewReportDto_today = new RealTimeOverviewReportDto();
            BeanUtils.copyProperties(realTimeOverviewReportDto, realTimeOverviewReportDto_today);
            realTimeOverviewReportDto_today.setMerId(realTimeOverviewReportDto.getMerId());
            realTimeOverviewReportDto_today.setDeviceType(realTimeOverviewReportDto.getDeviceType());
            realTimeOverviewReportDto_today.setDeviceUniqueIdList(realTimeOverviewReportDto.getDeviceUniqueIdList());
            realTimeOverviewReportDto_today.setPlatformType(realTimeOverviewReportDto.getPlatformType());
            realTimeOverviewReportDto_today.setQueryStartTime(todayStart);
            realTimeOverviewReportDto_today.setQueryEndTime(todayEnd);
            realTimeOverviewReportDto_today.setSubDeviceUniqueIdList(realTimeOverviewReportDto.getSubDeviceUniqueIdList());
            RealTimeOverviewReportVo realTimeOverviewReportVo_today = realTimeOverviewQuery(realTimeOverviewReportDto_today);
            realTimeOverviewResultVo.setToday(realTimeOverviewReportVo_today);

            //查询昨天的实时总览
            //昨天
            String yesterday = Tools.getYesterdayDateStr();
            //昨天开始时间
            Integer yesterdayStart = DateUtil.getStrTime(yesterday + " 00:00:00");
            //昨天的结束时间
            Integer yesterdayEnd = DateUtil.getStrTime(yesterday + " 23:59:59");
            //今天的参数对象
            RealTimeOverviewReportDto realTimeOverviewReportDto_yesterday = new RealTimeOverviewReportDto();
            BeanUtils.copyProperties(realTimeOverviewReportDto, realTimeOverviewReportDto_yesterday);
            realTimeOverviewReportDto_yesterday.setMerId(realTimeOverviewReportDto.getMerId());
            realTimeOverviewReportDto_yesterday.setDeviceType(realTimeOverviewReportDto.getDeviceType());
            realTimeOverviewReportDto_yesterday.setDeviceUniqueIdList(realTimeOverviewReportDto.getDeviceUniqueIdList());
            realTimeOverviewReportDto_yesterday.setPlatformType(realTimeOverviewReportDto.getPlatformType());
            realTimeOverviewReportDto_yesterday.setQueryStartTime(yesterdayStart);
            realTimeOverviewReportDto_yesterday.setQueryEndTime(yesterdayEnd);
            realTimeOverviewReportDto_yesterday.setSubDeviceUniqueIdList(realTimeOverviewReportDto.getSubDeviceUniqueIdList());
            RealTimeOverviewReportVo realTimeOverviewReportVo_yesterday = realTimeOverviewQuery(realTimeOverviewReportDto_yesterday);
            realTimeOverviewResultVo.setYesterday(realTimeOverviewReportVo_yesterday);

            //查询本月的实时总览
            //获取本月第一天
            String month_start = DateUtil.getEndDayOrOneDay(new Date(),0);
            //本月第一天的开始时间戳
            Integer monthStart = DateUtil.getStrTime(month_start + " 00:00:00");
            //获取本月最后一天
            String month_end = DateUtil.getEndDayOrOneDay(new Date(),1);
            //本月最后一天的结束时间戳
            Integer monthEnd = DateUtil.getStrTime(month_end + " 23:59:59");
            //本月的参数对象
            RealTimeOverviewReportDto realTimeOverviewReportDto_month = new RealTimeOverviewReportDto();
            BeanUtils.copyProperties(realTimeOverviewReportDto, realTimeOverviewReportDto_month);
            realTimeOverviewReportDto_month.setMerId(realTimeOverviewReportDto.getMerId());
            realTimeOverviewReportDto_month.setDeviceType(realTimeOverviewReportDto.getDeviceType());
            realTimeOverviewReportDto_month.setDeviceUniqueIdList(realTimeOverviewReportDto.getDeviceUniqueIdList());
            realTimeOverviewReportDto_month.setPlatformType(realTimeOverviewReportDto.getPlatformType());
            realTimeOverviewReportDto_month.setQueryStartTime(monthStart);
            realTimeOverviewReportDto_month.setQueryEndTime(monthEnd);
            realTimeOverviewReportDto_month.setSubDeviceUniqueIdList(realTimeOverviewReportDto.getSubDeviceUniqueIdList());
            RealTimeOverviewReportVo realTimeOverviewReportVo_month = realTimeOverviewQuery(realTimeOverviewReportDto_month);
            realTimeOverviewResultVo.setCurrentMonth(realTimeOverviewReportVo_month);
        }else{
            //只查询时间段内的数据
            RealTimeOverviewReportVo realTimeOverviewReportVo = realTimeOverviewQuery(realTimeOverviewReportDto);
            realTimeOverviewResultVo.setToday(realTimeOverviewReportVo);
        }

        return realTimeOverviewResultVo;
    }


    //查询某个时间段内的实时总览数据
    private RealTimeOverviewReportVo realTimeOverviewQuery(RealTimeOverviewReportDto realTimeOverviewReportDto){

        //查询缓存
        String redisKey = realTimeOverviewReportDto.signTopRequest();
        //查询缓存中是否报表
        Object object = redisTemplate.opsForValue().get(String.format(CacheKey.REPORT_REALTIMEOVERVIEW, redisKey));
        if(object != null){
            RealTimeOverviewReportVo realTimeOverviewReportVo = JSONObject.parseObject(object.toString(), RealTimeOverviewReportVo.class);
            return realTimeOverviewReportVo;
        }

        //是否页面选择了机器人-如果页面选择了机器人-则总预估佣金、总预估利润、有效订单笔数 取绑定绑定机器人数值
        boolean bandedDeviceFlag = false;

        RealTimeOverviewReportVo realTimeOverviewReportVo = new RealTimeOverviewReportVo();

        Long merId = realTimeOverviewReportDto.getMerId();
        String deviceType = realTimeOverviewReportDto.getDeviceType();
        //机器人查询条件
        List<Long> deviceUniqueIdList = realTimeOverviewReportDto.getDeviceUniqueIdList();
        //查询机器人不为空，则绑定了机器人
        if(deviceUniqueIdList != null && deviceUniqueIdList.size() > 0){
            bandedDeviceFlag = true;
        }

        //查询开始时间
        Integer queryStartTime = realTimeOverviewReportDto.getQueryStartTime();
        //查询结束时间
        Integer queryEndTime = realTimeOverviewReportDto.getQueryEndTime();
        //查询平台（默认查询全部）
        Integer platformType = Tools.getInteger(realTimeOverviewReportDto.getPlatformType());
        //判断查询条件
        if(merId == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "系统参数错误");
        }

        if(StringUtils.isBlank(deviceType)){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择设备类型");
        }

        if(queryStartTime == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入时间范围");
        }

        if(queryEndTime == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入时间范围");
        }

        int now = DateUtil.getNowTime();

        if(queryEndTime.intValue() < queryStartTime.intValue()){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "结束时间不能早于开始时间");
        }

        if(queryEndTime.intValue()- queryStartTime.intValue() > 31*24*3600){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "最多只能查询31天的报表");
        }

        if(now - queryStartTime.intValue() > 90*24*3600){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "最早只能查询90天以内的报表");
        }

        //新建返回到页面的LIST对象
        //封装微服务机器人参数
        List<Long> robotIds = new ArrayList<>();

        //个人微信
        if(DeviceTypeEnum.DT_PC_WECHAT.getKey().equals(deviceType)){
            DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
            BeanUtils.copyProperties(realTimeOverviewReportDto, deviceWechatInfoQuery);
            deviceWechatInfoQuery.setIdList(deviceUniqueIdList);
            deviceWechatInfoQuery.setMerId(merId);
            ArrayList<DeviceWechatInfo> deviceWechatInfoList =  deviceWechatInfoMapper.findByList(deviceWechatInfoQuery);
            if(deviceWechatInfoList != null && deviceWechatInfoList.size() > 0){
                for (DeviceWechatInfo deviceWechatInfo : deviceWechatInfoList){
                    robotIds.add(deviceWechatInfo.getId());
                }
            }
        }else if(DeviceTypeEnum.DT_WORK_WECHAT.getKey().equals(deviceType)){
            Page<DeviceWxworkInfo> page = new Page<>();
            DeviceWxworkInfoQuery deviceWxworkInfoQuery = new DeviceWxworkInfoQuery();
            BeanUtils.copyProperties(realTimeOverviewReportDto, deviceWxworkInfoQuery);
            deviceWxworkInfoQuery.setIdList(deviceUniqueIdList);
            deviceWxworkInfoQuery.setMerId(merId);
            ArrayList<DeviceWxworkInfo> deviceWxworkInfoList =  deviceWxworkInfoMapper.findByList(deviceWxworkInfoQuery);
            if(deviceWxworkInfoList != null && deviceWxworkInfoList.size() > 0){
                for (DeviceWxworkInfo deviceWxworkInfo : deviceWxworkInfoList){
                    robotIds.add(deviceWxworkInfo.getId());
                }
            }
        }else if(DeviceTypeEnum.WX_PUB.getKey().equals(deviceType)){
            WxpubInfoListQuery wxpubInfoListQuery = new WxpubInfoListQuery();
            BeanUtils.copyProperties(realTimeOverviewReportDto, wxpubInfoListQuery);
            wxpubInfoListQuery.setIdList(deviceUniqueIdList);
            wxpubInfoListQuery.setMerId(merId);
            List<WxpubInfoEntity> wxpubInfoEntityList =  wxpubInfoMapper.queryList(wxpubInfoListQuery);
            if(wxpubInfoEntityList != null && wxpubInfoEntityList.size() > 0){
                for (WxpubInfoEntity wxpubInfoEntity : wxpubInfoEntityList){
                    robotIds.add(wxpubInfoEntity.getId());
                }
            }
        }

        //有机器人才去查询
        if(robotIds != null && robotIds.size() > 0){

            //封装微服务机器人参数
            List<Long> robotIds_new = new ArrayList<>();
            for(Long deviceUniqueId : robotIds){
                //子账号负责的机器人列表
                List<Long> subDeviceUniqueIdList = realTimeOverviewReportDto.getSubDeviceUniqueIdList();
                if(subDeviceUniqueIdList != null && subDeviceUniqueIdList.size() > 0){
                    //子账号绑定了机器人
                    bandedDeviceFlag = true;
                    for (Long subDeviceUniqueId : subDeviceUniqueIdList){
                        if(subDeviceUniqueId.longValue() == deviceUniqueId.longValue()){
                            robotIds_new.add(deviceUniqueId);
                        }
                    }
                }else{
                    robotIds_new.add(deviceUniqueId);
                }
            }

            RealTimeOverviewReportFeignDto realTimeOverviewReportFeignDto = new RealTimeOverviewReportFeignDto();
            realTimeOverviewReportFeignDto.setStartTime(queryStartTime);
            realTimeOverviewReportFeignDto.setEndTime(queryEndTime);

            //判断是否绑定机器人
            if(bandedDeviceFlag){//绑定机器人才传送机器人列表参数
                realTimeOverviewReportFeignDto.setRobotIds(robotIds_new);
            }
            realTimeOverviewReportFeignDto.setPlatformType(platformType);
            realTimeOverviewReportFeignDto.setMerId(merId);
            realTimeOverviewReportFeignDto.setDeviceType(DeviceTypeEnum.getByKey(deviceType).getId());
            UzaiRespVo<RealTimeOverviewReportFeignVo> uzaiRespVo =  uzaiReportServerFeignService.realTimeOverviewSerach(realTimeOverviewReportFeignDto);
            //logger.info("----调用实时总览指标微服务---para={}, result={}", JSONObject.toJSONString(realTimeOverviewReportFeignDto), JSONObject.toJSONString(uzaiRespVo));
            if(uzaiRespVo != null && uzaiRespVo.getCode() == 200){
                RealTimeOverviewReportFeignVo realTimeOverviewReportFeignVo = uzaiRespVo.getData();
                if(realTimeOverviewReportFeignVo != null){
                    //对象赋值
                    BeanUtils.copyProperties(realTimeOverviewReportFeignVo, realTimeOverviewReportVo);

                    //绑定了机器人，则总预估佣金、总预估利润、有效订单笔数 取绑定绑定机器人数值
//                    if(bandedDeviceFlag){
//                        realTimeOverviewReportVo.setTotalEstimateProfit(realTimeOverviewReportFeignVo.getBindTotalEstimateProfit());
//                        realTimeOverviewReportVo.setTotalEstimateCommission(realTimeOverviewReportFeignVo.getBindTotalEstimateCommission());
//                        realTimeOverviewReportVo.setTotalValidOrderCount(realTimeOverviewReportFeignVo.getBindTotalValidOrderCount());
//                    }

                    //总预估利润
                    Double totalEstimateProfit = Tools.getDouble(realTimeOverviewReportVo.getTotalEstimateProfit());
                    //今日总奖励
                    Double totalRewards = Tools.getDouble(realTimeOverviewReportVo.getTotalRewards());
                    //总毛利
                    Double totalGrossRevenue = Math.sub(totalEstimateProfit.doubleValue(), totalRewards.doubleValue());
                    realTimeOverviewReportVo.setTotalGrossRevenue(totalGrossRevenue);

                    //查券总人数
                    Integer totalQueryVoucherUserCount = Tools.getInteger(realTimeOverviewReportVo.getTotalQueryVoucherUserCount());
                    //查券人数
                    Integer queryVoucherCount = Tools.getInteger(realTimeOverviewReportVo.getQueryVoucherCount());
                    if(totalQueryVoucherUserCount.intValue() != 0){
                        //查券率 = 查券人数/总人数
                        Double queryVoucherRate = Math.mul(Math.div(queryVoucherCount, totalQueryVoucherUserCount, 4), 100);
                        realTimeOverviewReportVo.setQueryVoucherRate(queryVoucherRate);
                    }else{
                        realTimeOverviewReportVo.setQueryVoucherRate(0.00d);
                    }

                    //下单总人数
                    Integer totalPlaceOrderUserCount = Tools.getInteger(realTimeOverviewReportVo.getTotalPlaceOrderUserCount());
                    //下单人数
                    Integer placeOrderCount =  Tools.getInteger(realTimeOverviewReportVo.getPlaceOrderCount());
                    if(totalPlaceOrderUserCount.intValue() != 0){
                        //下单率 = 下单人数/总人数
                        Double placeOrderRate = Math.mul(Math.div(placeOrderCount, totalPlaceOrderUserCount, 4), 100);
                        realTimeOverviewReportVo.setPlaceOrderRate(placeOrderRate);
                    }else{
                        realTimeOverviewReportVo.setPlaceOrderRate(0.00d);
                    }

                    //提现总金额
                    UserTakeBalQuery userTakeBalQuery_success = new UserTakeBalQuery();
                    userTakeBalQuery_success.setMerId(merId);
                    userTakeBalQuery_success.setDeviceUniqueIdList(robotIds);
                    userTakeBalQuery_success.setQueryStartTime(queryStartTime);
                    userTakeBalQuery_success.setQueryEndTime(queryEndTime);
                    //自动提现成功和手动审核成功
                    userTakeBalQuery_success.setStatusList(Arrays.asList(TakeBalStatusEnum.TBS_AUTO_TAKEBAL_SUCCESS.getValue(), TakeBalStatusEnum.TBS_MANUAL_AUDIT_SUCCESS.getValue()));
                    UserTakeBalReportVo userTakeBalReportVo_success = userTakeBalMapper.selectUserTakeBalReport(userTakeBalQuery_success);
                    if(userTakeBalReportVo_success != null){
                        //提现总金额
                        Double withdrawalTotalBalance = Tools.getDouble(userTakeBalReportVo_success.getTakeBalTotal());
                        realTimeOverviewReportVo.setWithdrawalTotalBalance(withdrawalTotalBalance);
                    }
                    //提现待处理金额
                    UserTakeBalQuery userTakeBalQuery_pending = new UserTakeBalQuery();
                    userTakeBalQuery_pending.setMerId(merId);
                    userTakeBalQuery_pending.setDeviceUniqueIdList(robotIds);
                    userTakeBalQuery_pending.setQueryStartTime(queryStartTime);
                    userTakeBalQuery_pending.setQueryEndTime(queryEndTime);
                    //待处理
                    userTakeBalQuery_pending.setStatusList(Lists.newArrayList(TakeBalStatusEnum.TBS_PENDING.getValue(), TakeBalStatusEnum.TBS_AUTO_TAKEBAL_FAIL.getValue())); //待处理和自动提现失败
                    UserTakeBalReportVo userTakeBalReportVo_pending = userTakeBalMapper.selectUserTakeBalReport(userTakeBalQuery_pending);
                    if(userTakeBalReportVo_pending != null){
                        //待处理总金额
                        Double withdrawalPendingBalance = Tools.getDouble(userTakeBalReportVo_pending.getTakeBalTotal());
                        realTimeOverviewReportVo.setWithdrawalPendingBalance(withdrawalPendingBalance);
                        //待处理笔数
                        Integer withdrawalPendingNumber = Tools.getInteger(userTakeBalReportVo_pending.getTakeBalCount());
                        realTimeOverviewReportVo.setWithdrawalPendingNumber(withdrawalPendingNumber);
                    }

                    //更新缓存
                    redisTemplate.opsForValue().set(String.format(CacheKey.REPORT_REALTIMEOVERVIEW, redisKey), Tools.getStrEmpty(JSONObject.toJSONString(realTimeOverviewReportVo)), 2, TimeUnit.MINUTES);
                }
            }
        }
        return realTimeOverviewReportVo;
    }

    /**
     * 实时总览报表-利润佣金
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param realTimeOverviewReportDto
     * @return null
     */
    public RealTimeOverviewResultVo realTimeOverviewReport_commission(RealTimeOverviewReportDto realTimeOverviewReportDto){

        //返回到页面结果
        RealTimeOverviewResultVo realTimeOverviewResultVo = new RealTimeOverviewResultVo();
        //查询开始时间
        Integer queryStartTime = realTimeOverviewReportDto.getQueryStartTime();
        //查询结束时间
        Integer queryEndTime = realTimeOverviewReportDto.getQueryEndTime();

        //查询时间范围为空，则查询今天、昨天、本月的数据
        if(queryStartTime == null || queryEndTime == null){
            //查询今天的实时总览
            //今天
            String today = Tools.getDateMonthDayStrTwo();
            //今天的开始时间戳
            Integer todayStart = DateUtil.getStrTime(today + " 00:00:00");
            //今天的结束时间错
            Integer todayEnd = DateUtil.getStrTime(today + " 23:59:59");
            //今天的参数对象
            RealTimeOverviewReportDto realTimeOverviewReportDto_today = new RealTimeOverviewReportDto();
            BeanUtils.copyProperties(realTimeOverviewReportDto, realTimeOverviewReportDto_today);
            realTimeOverviewReportDto_today.setMerId(realTimeOverviewReportDto.getMerId());
            realTimeOverviewReportDto_today.setDeviceType(realTimeOverviewReportDto.getDeviceType());
            realTimeOverviewReportDto_today.setDeviceUniqueIdList(realTimeOverviewReportDto.getDeviceUniqueIdList());
            realTimeOverviewReportDto_today.setPlatformType(realTimeOverviewReportDto.getPlatformType());
            realTimeOverviewReportDto_today.setQueryStartTime(todayStart);
            realTimeOverviewReportDto_today.setQueryEndTime(todayEnd);
            realTimeOverviewReportDto_today.setSubDeviceUniqueIdList(realTimeOverviewReportDto.getSubDeviceUniqueIdList());
            RealTimeOverviewReportVo realTimeOverviewReportVo_today = realTimeOverviewQuery_commission(realTimeOverviewReportDto_today);
            realTimeOverviewResultVo.setToday(realTimeOverviewReportVo_today);

            //查询昨天的实时总览
            //昨天
            String yesterday = Tools.getYesterdayDateStr();
            //昨天开始时间
            Integer yesterdayStart = DateUtil.getStrTime(yesterday + " 00:00:00");
            //昨天的结束时间
            Integer yesterdayEnd = DateUtil.getStrTime(yesterday + " 23:59:59");
            //今天的参数对象
            RealTimeOverviewReportDto realTimeOverviewReportDto_yesterday = new RealTimeOverviewReportDto();
            BeanUtils.copyProperties(realTimeOverviewReportDto, realTimeOverviewReportDto_yesterday);
            realTimeOverviewReportDto_yesterday.setMerId(realTimeOverviewReportDto.getMerId());
            realTimeOverviewReportDto_yesterday.setDeviceType(realTimeOverviewReportDto.getDeviceType());
            realTimeOverviewReportDto_yesterday.setDeviceUniqueIdList(realTimeOverviewReportDto.getDeviceUniqueIdList());
            realTimeOverviewReportDto_yesterday.setPlatformType(realTimeOverviewReportDto.getPlatformType());
            realTimeOverviewReportDto_yesterday.setQueryStartTime(yesterdayStart);
            realTimeOverviewReportDto_yesterday.setQueryEndTime(yesterdayEnd);
            realTimeOverviewReportDto_yesterday.setSubDeviceUniqueIdList(realTimeOverviewReportDto.getSubDeviceUniqueIdList());
            RealTimeOverviewReportVo realTimeOverviewReportVo_yesterday = realTimeOverviewQuery_commission(realTimeOverviewReportDto_yesterday);
            realTimeOverviewResultVo.setYesterday(realTimeOverviewReportVo_yesterday);

            //查询本月的实时总览
            //获取本月第一天
            String month_start = DateUtil.getEndDayOrOneDay(new Date(),0);
            //本月第一天的开始时间戳
            Integer monthStart = DateUtil.getStrTime(month_start + " 00:00:00");
            //获取本月最后一天
            String month_end = DateUtil.getEndDayOrOneDay(new Date(),1);
            //本月最后一天的结束时间戳
            Integer monthEnd = DateUtil.getStrTime(month_end + " 23:59:59");
            //本月的参数对象
            RealTimeOverviewReportDto realTimeOverviewReportDto_month = new RealTimeOverviewReportDto();
            BeanUtils.copyProperties(realTimeOverviewReportDto, realTimeOverviewReportDto_month);
            realTimeOverviewReportDto_month.setMerId(realTimeOverviewReportDto.getMerId());
            realTimeOverviewReportDto_month.setDeviceType(realTimeOverviewReportDto.getDeviceType());
            realTimeOverviewReportDto_month.setDeviceUniqueIdList(realTimeOverviewReportDto.getDeviceUniqueIdList());
            realTimeOverviewReportDto_month.setPlatformType(realTimeOverviewReportDto.getPlatformType());
            realTimeOverviewReportDto_month.setQueryStartTime(monthStart);
            realTimeOverviewReportDto_month.setQueryEndTime(monthEnd);
            realTimeOverviewReportDto_month.setSubDeviceUniqueIdList(realTimeOverviewReportDto.getSubDeviceUniqueIdList());
            RealTimeOverviewReportVo realTimeOverviewReportVo_month = realTimeOverviewQuery_commission(realTimeOverviewReportDto_month);
            realTimeOverviewResultVo.setCurrentMonth(realTimeOverviewReportVo_month);
        }else{
            //只查询时间段内的数据
            RealTimeOverviewReportVo realTimeOverviewReportVo = realTimeOverviewQuery_commission(realTimeOverviewReportDto);
            realTimeOverviewResultVo.setToday(realTimeOverviewReportVo);
        }

        return realTimeOverviewResultVo;
    }


    //查询某个时间段内的实时总览数据
    private RealTimeOverviewReportVo realTimeOverviewQuery_commission(RealTimeOverviewReportDto realTimeOverviewReportDto){
        //查询缓存
        String redisKey = realTimeOverviewReportDto.signTopRequest();
        //查询缓存中是否报表
        Object object = redisTemplate.opsForValue().get(String.format(CacheKey.REPORT_REALTIMEOVERVIEW_COMMISSION, redisKey));
        if(object != null){
            RealTimeOverviewReportVo realTimeOverviewReportVo = JSONObject.parseObject(object.toString(), RealTimeOverviewReportVo.class);
            return realTimeOverviewReportVo;
        }

        //是否页面选择了机器人-如果页面选择了机器人-则总预估佣金、总预估利润、有效订单笔数 取绑定绑定机器人数值
        boolean bandedDeviceFlag = false;

        RealTimeOverviewReportVo realTimeOverviewReportVo = new RealTimeOverviewReportVo();

        Long merId = realTimeOverviewReportDto.getMerId();
        String deviceType = realTimeOverviewReportDto.getDeviceType();
        //机器人查询条件
        List<Long> deviceUniqueIdList = realTimeOverviewReportDto.getDeviceUniqueIdList();
        //查询机器人不为空，则绑定了机器人
        if(deviceUniqueIdList != null && deviceUniqueIdList.size() > 0){
            bandedDeviceFlag = true;
        }

        //查询开始时间
        Integer queryStartTime = realTimeOverviewReportDto.getQueryStartTime();
        //查询结束时间
        Integer queryEndTime = realTimeOverviewReportDto.getQueryEndTime();
        //查询平台（默认查询全部）
        Integer platformType = Tools.getInteger(realTimeOverviewReportDto.getPlatformType());
        //判断查询条件
        if(merId == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "系统参数错误");
        }

        if(StringUtils.isBlank(deviceType)){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择设备类型");
        }

        if(queryStartTime == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入时间范围");
        }

        if(queryEndTime == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入时间范围");
        }

        int now = DateUtil.getNowTime();

        if(queryEndTime.intValue() < queryStartTime.intValue()){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "结束时间不能早于开始时间");
        }

        if(queryEndTime.intValue()- queryStartTime.intValue() > 31*24*3600){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "最多只能查询31天的报表");
        }

        if(now - queryStartTime.intValue() > 90*24*3600){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "最早只能查询90天以内的报表");
        }

        //新建返回到页面的LIST对象
        //封装微服务机器人参数
        List<Long> robotIds = new ArrayList<>();

        //个人微信
        if(DeviceTypeEnum.DT_PC_WECHAT.getKey().equals(deviceType)){
            DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
            BeanUtils.copyProperties(realTimeOverviewReportDto, deviceWechatInfoQuery);
            deviceWechatInfoQuery.setIdList(deviceUniqueIdList);
            deviceWechatInfoQuery.setMerId(merId);
            ArrayList<DeviceWechatInfo> deviceWechatInfoList =  deviceWechatInfoMapper.findByList(deviceWechatInfoQuery);
            if(deviceWechatInfoList != null && deviceWechatInfoList.size() > 0){
                for (DeviceWechatInfo deviceWechatInfo : deviceWechatInfoList){
                    robotIds.add(deviceWechatInfo.getId());
                }
            }
        }else if(DeviceTypeEnum.DT_WORK_WECHAT.getKey().equals(deviceType)){
            Page<DeviceWxworkInfo> page = new Page<>();
            DeviceWxworkInfoQuery deviceWxworkInfoQuery = new DeviceWxworkInfoQuery();
            BeanUtils.copyProperties(realTimeOverviewReportDto, deviceWxworkInfoQuery);
            deviceWxworkInfoQuery.setIdList(deviceUniqueIdList);
            deviceWxworkInfoQuery.setMerId(merId);
            ArrayList<DeviceWxworkInfo> deviceWxworkInfoList =  deviceWxworkInfoMapper.findByList(deviceWxworkInfoQuery);
            if(deviceWxworkInfoList != null && deviceWxworkInfoList.size() > 0){
                for (DeviceWxworkInfo deviceWxworkInfo : deviceWxworkInfoList){
                    robotIds.add(deviceWxworkInfo.getId());
                }
            }
        }else if(DeviceTypeEnum.WX_PUB.getKey().equals(deviceType)){
            WxpubInfoListQuery wxpubInfoListQuery = new WxpubInfoListQuery();
            BeanUtils.copyProperties(realTimeOverviewReportDto, wxpubInfoListQuery);
            wxpubInfoListQuery.setIdList(deviceUniqueIdList);
            wxpubInfoListQuery.setMerId(merId);
            List<WxpubInfoEntity> wxpubInfoEntityList =  wxpubInfoMapper.queryList(wxpubInfoListQuery);
            if(wxpubInfoEntityList != null && wxpubInfoEntityList.size() > 0){
                for (WxpubInfoEntity wxpubInfoEntity : wxpubInfoEntityList){
                    robotIds.add(wxpubInfoEntity.getId());
                }
            }
        }

        //有机器人才去查询
        if(robotIds != null && robotIds.size() > 0){
            //封装微服务机器人参数
            List<Long> robotIds_new = new ArrayList<>();
            for(Long deviceUniqueId : robotIds){
                //子账号负责的机器人列表
                List<Long> subDeviceUniqueIdList = realTimeOverviewReportDto.getSubDeviceUniqueIdList();
                if(subDeviceUniqueIdList != null && subDeviceUniqueIdList.size() > 0){
                    //子账号绑定了机器人
                    bandedDeviceFlag = true;
                    for (Long subDeviceUniqueId : subDeviceUniqueIdList){
                        if(subDeviceUniqueId.longValue() == deviceUniqueId.longValue()){
                            robotIds_new.add(deviceUniqueId);
                        }
                    }
                }else{
                    robotIds_new.add(deviceUniqueId);
                }
            }

            RealTimeOverviewReportFeignDto realTimeOverviewReportFeignDto = new RealTimeOverviewReportFeignDto();
            realTimeOverviewReportFeignDto.setStartTime(queryStartTime);
            realTimeOverviewReportFeignDto.setEndTime(queryEndTime);

            //判断是否绑定机器人
            if(bandedDeviceFlag){//绑定机器人才传送机器人列表参数
                realTimeOverviewReportFeignDto.setRobotIds(robotIds_new);
            }
            realTimeOverviewReportFeignDto.setPlatformType(platformType);
            realTimeOverviewReportFeignDto.setMerId(merId);
            realTimeOverviewReportFeignDto.setDeviceType(DeviceTypeEnum.getByKey(deviceType).getId());
            UzaiRespVo<RealTimeOverviewReportFeignVo> uzaiRespVo =  uzaiReportServerFeignService.realTimeOverviewSerach_commission(realTimeOverviewReportFeignDto);
            //logger.info("----调用实时总览指标微服务---para={}, result={}", JSONObject.toJSONString(realTimeOverviewReportFeignDto), JSONObject.toJSONString(uzaiRespVo));
            if(uzaiRespVo != null && uzaiRespVo.getCode() == 200){
                RealTimeOverviewReportFeignVo realTimeOverviewReportFeignVo = uzaiRespVo.getData();
                if(realTimeOverviewReportFeignVo != null){
                    //对象赋值
                    BeanUtils.copyProperties(realTimeOverviewReportFeignVo, realTimeOverviewReportVo);

                    //绑定了机器人，则总预估佣金、总预估利润、有效订单笔数 取绑定绑定机器人数值
//                    if(bandedDeviceFlag){
//                        realTimeOverviewReportVo.setTotalEstimateProfit(realTimeOverviewReportFeignVo.getBindTotalEstimateProfit());
//                        realTimeOverviewReportVo.setTotalEstimateCommission(realTimeOverviewReportFeignVo.getBindTotalEstimateCommission());
//                        realTimeOverviewReportVo.setTotalValidOrderCount(realTimeOverviewReportFeignVo.getBindTotalValidOrderCount());
//                    }

                    //总预估利润
                    Double totalEstimateProfit = Tools.getDouble(realTimeOverviewReportVo.getTotalEstimateProfit());
                    //今日总奖励
                    Double totalRewards = Tools.getDouble(realTimeOverviewReportVo.getTotalRewards());
                    //总毛利
                    Double totalGrossRevenue = Math.sub(totalEstimateProfit.doubleValue(), totalRewards.doubleValue());
                    realTimeOverviewReportVo.setTotalGrossRevenue(totalGrossRevenue);

                    //查券总人数
                    Integer totalQueryVoucherUserCount = Tools.getInteger(realTimeOverviewReportVo.getTotalQueryVoucherUserCount());
                    //查券人数
                    Integer queryVoucherCount = Tools.getInteger(realTimeOverviewReportVo.getQueryVoucherCount());
                    if(totalQueryVoucherUserCount.intValue() != 0){
                        //查券率 = 查券人数/总人数
                        Double queryVoucherRate = Math.mul(Math.div(queryVoucherCount, totalQueryVoucherUserCount, 4), 100);
                        realTimeOverviewReportVo.setQueryVoucherRate(queryVoucherRate);
                    }else{
                        realTimeOverviewReportVo.setQueryVoucherRate(0.00d);
                    }

                    //下单总人数
                    Integer totalPlaceOrderUserCount = Tools.getInteger(realTimeOverviewReportVo.getTotalPlaceOrderUserCount());
                    //下单人数
                    Integer placeOrderCount =  Tools.getInteger(realTimeOverviewReportVo.getPlaceOrderCount());
                    if(totalPlaceOrderUserCount.intValue() != 0){
                        //下单率 = 下单人数/总人数
                        Double placeOrderRate = Math.mul(Math.div(placeOrderCount, totalPlaceOrderUserCount, 4), 100);
                        realTimeOverviewReportVo.setPlaceOrderRate(placeOrderRate);
                    }else{
                        realTimeOverviewReportVo.setPlaceOrderRate(0.00d);
                    }
                    //更新缓存
                    redisTemplate.opsForValue().set(String.format(CacheKey.REPORT_REALTIMEOVERVIEW_COMMISSION, redisKey), Tools.getStrEmpty(JSONObject.toJSONString(realTimeOverviewReportVo)), 2, TimeUnit.MINUTES);
                }
            }
        }
        return realTimeOverviewReportVo;
    }

    /**
     * 实时总览报表-进粉数据-统计页面第二块
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param realTimeOverviewReportDto
     * @return null
     */
    public RealTimeOverviewResultVo realTimeOverviewReport_addFans(RealTimeOverviewReportDto realTimeOverviewReportDto){

        //返回到页面结果
        RealTimeOverviewResultVo realTimeOverviewResultVo = new RealTimeOverviewResultVo();
        //查询开始时间
        Integer queryStartTime = realTimeOverviewReportDto.getQueryStartTime();
        //查询结束时间
        Integer queryEndTime = realTimeOverviewReportDto.getQueryEndTime();

        //查询时间范围为空，则查询今天、昨天、本月的数据
        if(queryStartTime == null || queryEndTime == null){
            //查询今天的实时总览
            //今天
            String today = Tools.getDateMonthDayStrTwo();
            //今天的开始时间戳
            Integer todayStart = DateUtil.getStrTime(today + " 00:00:00");
            //今天的结束时间错
            Integer todayEnd = DateUtil.getStrTime(today + " 23:59:59");
            //今天的参数对象
            RealTimeOverviewReportDto realTimeOverviewReportDto_today = new RealTimeOverviewReportDto();
            BeanUtils.copyProperties(realTimeOverviewReportDto, realTimeOverviewReportDto_today);
            realTimeOverviewReportDto_today.setMerId(realTimeOverviewReportDto.getMerId());
            realTimeOverviewReportDto_today.setDeviceType(realTimeOverviewReportDto.getDeviceType());
            realTimeOverviewReportDto_today.setDeviceUniqueIdList(realTimeOverviewReportDto.getDeviceUniqueIdList());
            realTimeOverviewReportDto_today.setPlatformType(realTimeOverviewReportDto.getPlatformType());
            realTimeOverviewReportDto_today.setQueryStartTime(todayStart);
            realTimeOverviewReportDto_today.setQueryEndTime(todayEnd);
            realTimeOverviewReportDto_today.setSubDeviceUniqueIdList(realTimeOverviewReportDto.getSubDeviceUniqueIdList());
            RealTimeOverviewReportVo realTimeOverviewReportVo_today = realTimeOverviewQuery_addFans(realTimeOverviewReportDto_today);
            realTimeOverviewResultVo.setToday(realTimeOverviewReportVo_today);

            //查询昨天的实时总览
            //昨天
            String yesterday = Tools.getYesterdayDateStr();
            //昨天开始时间
            Integer yesterdayStart = DateUtil.getStrTime(yesterday + " 00:00:00");
            //昨天的结束时间
            Integer yesterdayEnd = DateUtil.getStrTime(yesterday + " 23:59:59");
            //今天的参数对象
            RealTimeOverviewReportDto realTimeOverviewReportDto_yesterday = new RealTimeOverviewReportDto();
            BeanUtils.copyProperties(realTimeOverviewReportDto, realTimeOverviewReportDto_yesterday);
            realTimeOverviewReportDto_yesterday.setMerId(realTimeOverviewReportDto.getMerId());
            realTimeOverviewReportDto_yesterday.setDeviceType(realTimeOverviewReportDto.getDeviceType());
            realTimeOverviewReportDto_yesterday.setDeviceUniqueIdList(realTimeOverviewReportDto.getDeviceUniqueIdList());
            realTimeOverviewReportDto_yesterday.setPlatformType(realTimeOverviewReportDto.getPlatformType());
            realTimeOverviewReportDto_yesterday.setQueryStartTime(yesterdayStart);
            realTimeOverviewReportDto_yesterday.setQueryEndTime(yesterdayEnd);
            realTimeOverviewReportDto_yesterday.setSubDeviceUniqueIdList(realTimeOverviewReportDto.getSubDeviceUniqueIdList());
            RealTimeOverviewReportVo realTimeOverviewReportVo_yesterday = realTimeOverviewQuery_addFans(realTimeOverviewReportDto_yesterday);
            realTimeOverviewResultVo.setYesterday(realTimeOverviewReportVo_yesterday);

            //查询本月的实时总览
            //获取本月第一天
            String month_start = DateUtil.getEndDayOrOneDay(new Date(),0);
            //本月第一天的开始时间戳
            Integer monthStart = DateUtil.getStrTime(month_start + " 00:00:00");
            //获取本月最后一天
            String month_end = DateUtil.getEndDayOrOneDay(new Date(),1);
            //本月最后一天的结束时间戳
            Integer monthEnd = DateUtil.getStrTime(month_end + " 23:59:59");
            //本月的参数对象
            RealTimeOverviewReportDto realTimeOverviewReportDto_month = new RealTimeOverviewReportDto();
            BeanUtils.copyProperties(realTimeOverviewReportDto, realTimeOverviewReportDto_month);
            realTimeOverviewReportDto_month.setMerId(realTimeOverviewReportDto.getMerId());
            realTimeOverviewReportDto_month.setDeviceType(realTimeOverviewReportDto.getDeviceType());
            realTimeOverviewReportDto_month.setDeviceUniqueIdList(realTimeOverviewReportDto.getDeviceUniqueIdList());
            realTimeOverviewReportDto_month.setPlatformType(realTimeOverviewReportDto.getPlatformType());
            realTimeOverviewReportDto_month.setQueryStartTime(monthStart);
            realTimeOverviewReportDto_month.setQueryEndTime(monthEnd);
            realTimeOverviewReportDto_month.setSubDeviceUniqueIdList(realTimeOverviewReportDto.getSubDeviceUniqueIdList());
            RealTimeOverviewReportVo realTimeOverviewReportVo_month = realTimeOverviewQuery_addFans(realTimeOverviewReportDto_month);
            realTimeOverviewResultVo.setCurrentMonth(realTimeOverviewReportVo_month);
        }else{
            //只查询时间段内的数据
            RealTimeOverviewReportVo realTimeOverviewReportVo = realTimeOverviewQuery_addFans(realTimeOverviewReportDto);
            realTimeOverviewResultVo.setToday(realTimeOverviewReportVo);
        }

        return realTimeOverviewResultVo;
    }


    //查询某个时间段内的实时总览数据
    private RealTimeOverviewReportVo realTimeOverviewQuery_addFans(RealTimeOverviewReportDto realTimeOverviewReportDto){

        //查询缓存
        String redisKey = realTimeOverviewReportDto.signTopRequest();
        //查询缓存中是否报表
        Object object = redisTemplate.opsForValue().get(String.format(CacheKey.REPORT_REALTIMEOVERVIEW_ADDFANS, redisKey));
        if(object != null){
            RealTimeOverviewReportVo realTimeOverviewReportVo = JSONObject.parseObject(object.toString(), RealTimeOverviewReportVo.class);
            return realTimeOverviewReportVo;
        }

        //是否页面选择了机器人-如果页面选择了机器人-则总预估佣金、总预估利润、有效订单笔数 取绑定绑定机器人数值
        boolean bandedDeviceFlag = false;

        RealTimeOverviewReportVo realTimeOverviewReportVo = new RealTimeOverviewReportVo();

        Long merId = realTimeOverviewReportDto.getMerId();
        String deviceType = realTimeOverviewReportDto.getDeviceType();
        //机器人查询条件
        List<Long> deviceUniqueIdList = realTimeOverviewReportDto.getDeviceUniqueIdList();
        //查询机器人不为空，则绑定了机器人
        if(deviceUniqueIdList != null && deviceUniqueIdList.size() > 0){
            bandedDeviceFlag = true;
        }

        //查询开始时间
        Integer queryStartTime = realTimeOverviewReportDto.getQueryStartTime();
        //查询结束时间
        Integer queryEndTime = realTimeOverviewReportDto.getQueryEndTime();
        //查询平台（默认查询全部）
        Integer platformType = Tools.getInteger(realTimeOverviewReportDto.getPlatformType());
        //判断查询条件
        if(merId == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "系统参数错误");
        }

        if(StringUtils.isBlank(deviceType)){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择设备类型");
        }

        if(queryStartTime == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入时间范围");
        }

        if(queryEndTime == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入时间范围");
        }

        int now = DateUtil.getNowTime();

        if(queryEndTime.intValue() < queryStartTime.intValue()){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "结束时间不能早于开始时间");
        }

        if(queryEndTime.intValue()- queryStartTime.intValue() > 31*24*3600){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "最多只能查询31天的报表");
        }

        if(now - queryStartTime.intValue() > 90*24*3600){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "最早只能查询90天以内的报表");
        }

        //新建返回到页面的LIST对象
        //封装微服务机器人参数
        List<Long> robotIds = new ArrayList<>();

        //个人微信
        if(DeviceTypeEnum.DT_PC_WECHAT.getKey().equals(deviceType)){
            DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
            BeanUtils.copyProperties(realTimeOverviewReportDto, deviceWechatInfoQuery);
            deviceWechatInfoQuery.setIdList(deviceUniqueIdList);
            deviceWechatInfoQuery.setMerId(merId);
            ArrayList<DeviceWechatInfo> deviceWechatInfoList =  deviceWechatInfoMapper.findByList(deviceWechatInfoQuery);
            if(deviceWechatInfoList != null && deviceWechatInfoList.size() > 0){
                for (DeviceWechatInfo deviceWechatInfo : deviceWechatInfoList){
                    robotIds.add(deviceWechatInfo.getId());
                }
            }
        }else if(DeviceTypeEnum.DT_WORK_WECHAT.getKey().equals(deviceType)){
            Page<DeviceWxworkInfo> page = new Page<>();
            DeviceWxworkInfoQuery deviceWxworkInfoQuery = new DeviceWxworkInfoQuery();
            BeanUtils.copyProperties(realTimeOverviewReportDto, deviceWxworkInfoQuery);
            deviceWxworkInfoQuery.setIdList(deviceUniqueIdList);
            deviceWxworkInfoQuery.setMerId(merId);
            ArrayList<DeviceWxworkInfo> deviceWxworkInfoList =  deviceWxworkInfoMapper.findByList(deviceWxworkInfoQuery);
            if(deviceWxworkInfoList != null && deviceWxworkInfoList.size() > 0){
                for (DeviceWxworkInfo deviceWxworkInfo : deviceWxworkInfoList){
                    robotIds.add(deviceWxworkInfo.getId());
                }
            }
        }else if(DeviceTypeEnum.WX_PUB.getKey().equals(deviceType)){
            WxpubInfoListQuery wxpubInfoListQuery = new WxpubInfoListQuery();
            BeanUtils.copyProperties(realTimeOverviewReportDto, wxpubInfoListQuery);
            wxpubInfoListQuery.setIdList(deviceUniqueIdList);
            wxpubInfoListQuery.setMerId(merId);
            List<WxpubInfoEntity> wxpubInfoEntityList =  wxpubInfoMapper.queryList(wxpubInfoListQuery);
            if(wxpubInfoEntityList != null && wxpubInfoEntityList.size() > 0){
                for (WxpubInfoEntity wxpubInfoEntity : wxpubInfoEntityList){
                    robotIds.add(wxpubInfoEntity.getId());
                }
            }
        }

        //有机器人才去查询
        if(robotIds != null && robotIds.size() > 0){

            //封装微服务机器人参数
            List<Long> robotIds_new = new ArrayList<>();
            for(Long deviceUniqueId : robotIds){
                //子账号负责的机器人列表
                List<Long> subDeviceUniqueIdList = realTimeOverviewReportDto.getSubDeviceUniqueIdList();
                if(subDeviceUniqueIdList != null && subDeviceUniqueIdList.size() > 0){
                    //子账号绑定了机器人
                    bandedDeviceFlag = true;
                    for (Long subDeviceUniqueId : subDeviceUniqueIdList){
                        if(subDeviceUniqueId.longValue() == deviceUniqueId.longValue()){
                            robotIds_new.add(deviceUniqueId);
                        }
                    }
                }else{
                    robotIds_new.add(deviceUniqueId);
                }
            }

            RealTimeOverviewReportFeignDto realTimeOverviewReportFeignDto = new RealTimeOverviewReportFeignDto();
            realTimeOverviewReportFeignDto.setStartTime(queryStartTime);
            realTimeOverviewReportFeignDto.setEndTime(queryEndTime);

            //判断是否绑定机器人
            if(bandedDeviceFlag){//绑定机器人才传送机器人列表参数
                realTimeOverviewReportFeignDto.setRobotIds(robotIds_new);
            }
            realTimeOverviewReportFeignDto.setPlatformType(platformType);
            realTimeOverviewReportFeignDto.setMerId(merId);
            realTimeOverviewReportFeignDto.setDeviceType(DeviceTypeEnum.getByKey(deviceType).getId());
            UzaiRespVo<RealTimeOverviewReportFeignVo> uzaiRespVo =  uzaiReportServerFeignService.realTimeOverviewSerach_addFans(realTimeOverviewReportFeignDto);
            //logger.info("----调用实时总览指标微服务---para={}, result={}", JSONObject.toJSONString(realTimeOverviewReportFeignDto), JSONObject.toJSONString(uzaiRespVo));
            if(uzaiRespVo != null && uzaiRespVo.getCode() == 200){
                RealTimeOverviewReportFeignVo realTimeOverviewReportFeignVo = uzaiRespVo.getData();
                if(realTimeOverviewReportFeignVo != null){
                    //对象赋值
                    BeanUtils.copyProperties(realTimeOverviewReportFeignVo, realTimeOverviewReportVo);

                    //更新缓存
                    redisTemplate.opsForValue().set(String.format(CacheKey.REPORT_REALTIMEOVERVIEW_ADDFANS, redisKey), Tools.getStrEmpty(JSONObject.toJSONString(realTimeOverviewReportVo)), 2, TimeUnit.MINUTES);
                }
            }
        }
        return realTimeOverviewReportVo;
    }


    /**
     * 实时总览报表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param realTimeOverviewReportDto
     * @return null
     */
    public RealTimeOverviewResultVo realTimeOverviewReport_takeBal(RealTimeOverviewReportDto realTimeOverviewReportDto){

        //返回到页面结果
        RealTimeOverviewResultVo realTimeOverviewResultVo = new RealTimeOverviewResultVo();
        //查询开始时间
        Integer queryStartTime = realTimeOverviewReportDto.getQueryStartTime();
        //查询结束时间
        Integer queryEndTime = realTimeOverviewReportDto.getQueryEndTime();

        //查询时间范围为空，则查询今天、昨天、本月的数据
        if(queryStartTime == null || queryEndTime == null){
            //查询今天的实时总览
            //今天
            String today = Tools.getDateMonthDayStrTwo();
            //今天的开始时间戳
            Integer todayStart = DateUtil.getStrTime(today + " 00:00:00");
            //今天的结束时间错
            Integer todayEnd = DateUtil.getStrTime(today + " 23:59:59");
            //今天的参数对象
            RealTimeOverviewReportDto realTimeOverviewReportDto_today = new RealTimeOverviewReportDto();
            BeanUtils.copyProperties(realTimeOverviewReportDto, realTimeOverviewReportDto_today);
            realTimeOverviewReportDto_today.setMerId(realTimeOverviewReportDto.getMerId());
            realTimeOverviewReportDto_today.setDeviceType(realTimeOverviewReportDto.getDeviceType());
            realTimeOverviewReportDto_today.setDeviceUniqueIdList(realTimeOverviewReportDto.getDeviceUniqueIdList());
            realTimeOverviewReportDto_today.setPlatformType(realTimeOverviewReportDto.getPlatformType());
            realTimeOverviewReportDto_today.setQueryStartTime(todayStart);
            realTimeOverviewReportDto_today.setQueryEndTime(todayEnd);
            realTimeOverviewReportDto_today.setSubDeviceUniqueIdList(realTimeOverviewReportDto.getSubDeviceUniqueIdList());
            RealTimeOverviewReportVo realTimeOverviewReportVo_today = realTimeOverviewQuery_takeBal(realTimeOverviewReportDto_today);
            realTimeOverviewResultVo.setToday(realTimeOverviewReportVo_today);

            //查询昨天的实时总览
            //昨天
            String yesterday = Tools.getYesterdayDateStr();
            //昨天开始时间
            Integer yesterdayStart = DateUtil.getStrTime(yesterday + " 00:00:00");
            //昨天的结束时间
            Integer yesterdayEnd = DateUtil.getStrTime(yesterday + " 23:59:59");
            //今天的参数对象
            RealTimeOverviewReportDto realTimeOverviewReportDto_yesterday = new RealTimeOverviewReportDto();
            BeanUtils.copyProperties(realTimeOverviewReportDto, realTimeOverviewReportDto_yesterday);
            realTimeOverviewReportDto_yesterday.setMerId(realTimeOverviewReportDto.getMerId());
            realTimeOverviewReportDto_yesterday.setDeviceType(realTimeOverviewReportDto.getDeviceType());
            realTimeOverviewReportDto_yesterday.setDeviceUniqueIdList(realTimeOverviewReportDto.getDeviceUniqueIdList());
            realTimeOverviewReportDto_yesterday.setPlatformType(realTimeOverviewReportDto.getPlatformType());
            realTimeOverviewReportDto_yesterday.setQueryStartTime(yesterdayStart);
            realTimeOverviewReportDto_yesterday.setQueryEndTime(yesterdayEnd);
            realTimeOverviewReportDto_yesterday.setSubDeviceUniqueIdList(realTimeOverviewReportDto.getSubDeviceUniqueIdList());
            RealTimeOverviewReportVo realTimeOverviewReportVo_yesterday = realTimeOverviewQuery_takeBal(realTimeOverviewReportDto_yesterday);
            realTimeOverviewResultVo.setYesterday(realTimeOverviewReportVo_yesterday);

            //查询本月的实时总览
            //获取本月第一天
            String month_start = DateUtil.getEndDayOrOneDay(new Date(),0);
            //本月第一天的开始时间戳
            Integer monthStart = DateUtil.getStrTime(month_start + " 00:00:00");
            //获取本月最后一天
            String month_end = DateUtil.getEndDayOrOneDay(new Date(),1);
            //本月最后一天的结束时间戳
            Integer monthEnd = DateUtil.getStrTime(month_end + " 23:59:59");
            //本月的参数对象
            RealTimeOverviewReportDto realTimeOverviewReportDto_month = new RealTimeOverviewReportDto();
            BeanUtils.copyProperties(realTimeOverviewReportDto, realTimeOverviewReportDto_month);
            realTimeOverviewReportDto_month.setMerId(realTimeOverviewReportDto.getMerId());
            realTimeOverviewReportDto_month.setDeviceType(realTimeOverviewReportDto.getDeviceType());
            realTimeOverviewReportDto_month.setDeviceUniqueIdList(realTimeOverviewReportDto.getDeviceUniqueIdList());
            realTimeOverviewReportDto_month.setPlatformType(realTimeOverviewReportDto.getPlatformType());
            realTimeOverviewReportDto_month.setQueryStartTime(monthStart);
            realTimeOverviewReportDto_month.setQueryEndTime(monthEnd);
            realTimeOverviewReportDto_month.setSubDeviceUniqueIdList(realTimeOverviewReportDto.getSubDeviceUniqueIdList());
            RealTimeOverviewReportVo realTimeOverviewReportVo_month = realTimeOverviewQuery_takeBal(realTimeOverviewReportDto_month);
            realTimeOverviewResultVo.setCurrentMonth(realTimeOverviewReportVo_month);
        }else{
            //只查询时间段内的数据
            RealTimeOverviewReportVo realTimeOverviewReportVo = realTimeOverviewQuery_takeBal(realTimeOverviewReportDto);
            realTimeOverviewResultVo.setToday(realTimeOverviewReportVo);
        }

        return realTimeOverviewResultVo;
    }


    //查询某个时间段内的实时总览数据
    private RealTimeOverviewReportVo realTimeOverviewQuery_takeBal(RealTimeOverviewReportDto realTimeOverviewReportDto){

        //查询缓存
        String redisKey = realTimeOverviewReportDto.signTopRequest();
        //查询缓存中是否报表
        Object object = redisTemplate.opsForValue().get(String.format(CacheKey.REPORT_REALTIMEOVERVIEW_TAKEBAL, redisKey));
        if(object != null){
            RealTimeOverviewReportVo realTimeOverviewReportVo = JSONObject.parseObject(object.toString(), RealTimeOverviewReportVo.class);
            return realTimeOverviewReportVo;
        }

        //是否页面选择了机器人-如果页面选择了机器人-则总预估佣金、总预估利润、有效订单笔数 取绑定绑定机器人数值
        boolean bandedDeviceFlag = false;

        RealTimeOverviewReportVo realTimeOverviewReportVo = new RealTimeOverviewReportVo();

        Long merId = realTimeOverviewReportDto.getMerId();
        String deviceType = realTimeOverviewReportDto.getDeviceType();
        //机器人查询条件
        List<Long> deviceUniqueIdList = realTimeOverviewReportDto.getDeviceUniqueIdList();
        //查询机器人不为空，则绑定了机器人
        if(deviceUniqueIdList != null && deviceUniqueIdList.size() > 0){
            bandedDeviceFlag = true;
        }

        //查询开始时间
        Integer queryStartTime = realTimeOverviewReportDto.getQueryStartTime();
        //查询结束时间
        Integer queryEndTime = realTimeOverviewReportDto.getQueryEndTime();
        //查询平台（默认查询全部）
        Integer platformType = Tools.getInteger(realTimeOverviewReportDto.getPlatformType());
        //判断查询条件
        if(merId == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "系统参数错误");
        }

        if(StringUtils.isBlank(deviceType)){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择设备类型");
        }

        if(queryStartTime == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入时间范围");
        }

        if(queryEndTime == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入时间范围");
        }

        int now = DateUtil.getNowTime();

        if(queryEndTime.intValue() < queryStartTime.intValue()){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "结束时间不能早于开始时间");
        }

        if(queryEndTime.intValue()- queryStartTime.intValue() > 31*24*3600){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "最多只能查询31天的报表");
        }

        if(now - queryStartTime.intValue() > 90*24*3600){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "最早只能查询90天以内的报表");
        }

        //新建返回到页面的LIST对象
        //封装微服务机器人参数
        List<Long> robotIds = new ArrayList<>();

        //个人微信
        if(DeviceTypeEnum.DT_PC_WECHAT.getKey().equals(deviceType)){
            DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
            BeanUtils.copyProperties(realTimeOverviewReportDto, deviceWechatInfoQuery);
            deviceWechatInfoQuery.setIdList(deviceUniqueIdList);
            deviceWechatInfoQuery.setMerId(merId);
            ArrayList<DeviceWechatInfo> deviceWechatInfoList =  deviceWechatInfoMapper.findByList(deviceWechatInfoQuery);
            if(deviceWechatInfoList != null && deviceWechatInfoList.size() > 0){
                for (DeviceWechatInfo deviceWechatInfo : deviceWechatInfoList){
                    robotIds.add(deviceWechatInfo.getId());
                }
            }
        }else if(DeviceTypeEnum.DT_WORK_WECHAT.getKey().equals(deviceType)){
            Page<DeviceWxworkInfo> page = new Page<>();
            DeviceWxworkInfoQuery deviceWxworkInfoQuery = new DeviceWxworkInfoQuery();
            BeanUtils.copyProperties(realTimeOverviewReportDto, deviceWxworkInfoQuery);
            deviceWxworkInfoQuery.setIdList(deviceUniqueIdList);
            deviceWxworkInfoQuery.setMerId(merId);
            ArrayList<DeviceWxworkInfo> deviceWxworkInfoList =  deviceWxworkInfoMapper.findByList(deviceWxworkInfoQuery);
            if(deviceWxworkInfoList != null && deviceWxworkInfoList.size() > 0){
                for (DeviceWxworkInfo deviceWxworkInfo : deviceWxworkInfoList){
                    robotIds.add(deviceWxworkInfo.getId());
                }
            }
        }else if(DeviceTypeEnum.WX_PUB.getKey().equals(deviceType)){
            WxpubInfoListQuery wxpubInfoListQuery = new WxpubInfoListQuery();
            BeanUtils.copyProperties(realTimeOverviewReportDto, wxpubInfoListQuery);
            wxpubInfoListQuery.setIdList(deviceUniqueIdList);
            wxpubInfoListQuery.setMerId(merId);
            List<WxpubInfoEntity> wxpubInfoEntityList =  wxpubInfoMapper.queryList(wxpubInfoListQuery);
            if(wxpubInfoEntityList != null && wxpubInfoEntityList.size() > 0){
                for (WxpubInfoEntity wxpubInfoEntity : wxpubInfoEntityList){
                    robotIds.add(wxpubInfoEntity.getId());
                }
            }
        }

        //有机器人才去查询
        if(robotIds != null && robotIds.size() > 0){
            //封装微服务机器人参数
            List<Long> robotIds_new = new ArrayList<>();
            for(Long deviceUniqueId : robotIds){
                //子账号负责的机器人列表
                List<Long> subDeviceUniqueIdList = realTimeOverviewReportDto.getSubDeviceUniqueIdList();
                if(subDeviceUniqueIdList != null && subDeviceUniqueIdList.size() > 0){
                    //子账号绑定了机器人
                    bandedDeviceFlag = true;
                    for (Long subDeviceUniqueId : subDeviceUniqueIdList){
                        if(subDeviceUniqueId.longValue() == deviceUniqueId.longValue()){
                            robotIds_new.add(deviceUniqueId);
                        }
                    }
                }else{
                    robotIds_new.add(deviceUniqueId);
                }
            }

//            //提现总金额
//            UserTakeBalQuery userTakeBalQuery_success = new UserTakeBalQuery();
//            userTakeBalQuery_success.setMerId(merId);
//            userTakeBalQuery_success.setDeviceUniqueIdList(robotIds);
//            userTakeBalQuery_success.setQueryStartTime(queryStartTime);
//            userTakeBalQuery_success.setQueryEndTime(queryEndTime);
//            //自动提现成功和手动审核成功
//            userTakeBalQuery_success.setStatusList(Arrays.asList(TakeBalStatusEnum.TBS_AUTO_TAKEBAL_SUCCESS.getValue(), TakeBalStatusEnum.TBS_MANUAL_AUDIT_SUCCESS.getValue()));
//            UserTakeBalReportVo userTakeBalReportVo_success = userTakeBalMapper.selectUserTakeBalReport(userTakeBalQuery_success);
//            if(userTakeBalReportVo_success != null){
//                //提现总金额
//                Double withdrawalTotalBalance = Tools.getDouble(userTakeBalReportVo_success.getTakeBalTotal());
//                realTimeOverviewReportVo.setWithdrawalTotalBalance(withdrawalTotalBalance);
//            }
//            //提现待处理金额
//            UserTakeBalQuery userTakeBalQuery_pending = new UserTakeBalQuery();
//            userTakeBalQuery_pending.setMerId(merId);
//            userTakeBalQuery_pending.setDeviceUniqueIdList(robotIds);
//            userTakeBalQuery_pending.setQueryStartTime(queryStartTime);
//            userTakeBalQuery_pending.setQueryEndTime(queryEndTime);
//            //待处理
//            userTakeBalQuery_pending.setStatusList(Lists.newArrayList(TakeBalStatusEnum.TBS_PENDING.getValue(), TakeBalStatusEnum.TBS_AUTO_TAKEBAL_FAIL.getValue())); //待处理和自动提现失败
//            UserTakeBalReportVo userTakeBalReportVo_pending = userTakeBalMapper.selectUserTakeBalReport(userTakeBalQuery_pending);
//            if(userTakeBalReportVo_pending != null){
//                //待处理总金额
//                Double withdrawalPendingBalance = Tools.getDouble(userTakeBalReportVo_pending.getTakeBalTotal());
//                realTimeOverviewReportVo.setWithdrawalPendingBalance(withdrawalPendingBalance);
//                //待处理笔数
//                Integer withdrawalPendingNumber = Tools.getInteger(userTakeBalReportVo_pending.getTakeBalCount());
//                realTimeOverviewReportVo.setWithdrawalPendingNumber(withdrawalPendingNumber);
//            }





            RealTimeOverviewReportFeignDto realTimeOverviewReportFeignDto = new RealTimeOverviewReportFeignDto();
            realTimeOverviewReportFeignDto.setStartTime(queryStartTime);
            realTimeOverviewReportFeignDto.setEndTime(queryEndTime);

            //判断是否绑定机器人
            if(bandedDeviceFlag){//绑定机器人才传送机器人列表参数
                realTimeOverviewReportFeignDto.setRobotIds(robotIds_new);
            }
            realTimeOverviewReportFeignDto.setPlatformType(platformType);
            realTimeOverviewReportFeignDto.setMerId(merId);
            realTimeOverviewReportFeignDto.setDeviceType(DeviceTypeEnum.getByKey(deviceType).getId());
            UzaiRespVo<RealTimeOverviewReportFeignVo> uzaiRespVo =  uzaiReportServerFeignService.realTimeOverviewSerach_takeBal(realTimeOverviewReportFeignDto);
            //logger.info("----调用实时总览指标微服务---para={}, result={}", JSONObject.toJSONString(realTimeOverviewReportFeignDto), JSONObject.toJSONString(uzaiRespVo));
            if(uzaiRespVo != null && uzaiRespVo.getCode() == 200) {

                realTimeOverviewReportVo.setWithdrawalPendingBalance(uzaiRespVo.getData().getWithdrawalPendingBalance());
                realTimeOverviewReportVo.setWithdrawalPendingNumber(uzaiRespVo.getData().getWithdrawalPendingNumber());
                realTimeOverviewReportVo.setWithdrawalTotalBalance(uzaiRespVo.getData().getWithdrawalTotalBalance());

                realTimeOverviewReportVo.setMarketingRedPackPendingNumber(uzaiRespVo.getData().getMarketingRedPackPendingNumber());
                realTimeOverviewReportVo.setMarketingRedPackPendingAmount(uzaiRespVo.getData().getMarketingRedPackPendingAmount());
                realTimeOverviewReportVo.setMarketingRedPackTotalAmount(uzaiRespVo.getData().getMarketingRedPackTotalAmount());

                //更新缓存
                redisTemplate.opsForValue().set(String.format(CacheKey.REPORT_REALTIMEOVERVIEW_TAKEBAL, redisKey), Tools.getStrEmpty(JSONObject.toJSONString(realTimeOverviewReportVo)), 2, TimeUnit.MINUTES);
            }
        }
        return realTimeOverviewReportVo;
    }


    /**
     * 实时总览报表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param realTimeOverviewReportDto
     * @return null
     */
    public RealTimeOverviewResultVo realTimeOverviewReport_expend(RealTimeOverviewReportDto realTimeOverviewReportDto){

        //返回到页面结果
        RealTimeOverviewResultVo realTimeOverviewResultVo = new RealTimeOverviewResultVo();
        //查询开始时间
        Integer queryStartTime = realTimeOverviewReportDto.getQueryStartTime();
        //查询结束时间
        Integer queryEndTime = realTimeOverviewReportDto.getQueryEndTime();

        //查询时间范围为空，则查询今天、昨天、本月的数据
        if(queryStartTime == null || queryEndTime == null){
            //查询今天的实时总览
            //今天
            String today = Tools.getDateMonthDayStrTwo();
            //今天的开始时间戳
            Integer todayStart = DateUtil.getStrTime(today + " 00:00:00");
            //今天的结束时间错
            Integer todayEnd = DateUtil.getStrTime(today + " 23:59:59");
            //今天的参数对象
            RealTimeOverviewReportDto realTimeOverviewReportDto_today = new RealTimeOverviewReportDto();
            BeanUtils.copyProperties(realTimeOverviewReportDto, realTimeOverviewReportDto_today);
            realTimeOverviewReportDto_today.setMerId(realTimeOverviewReportDto.getMerId());
            realTimeOverviewReportDto_today.setDeviceType(realTimeOverviewReportDto.getDeviceType());
            realTimeOverviewReportDto_today.setDeviceUniqueIdList(realTimeOverviewReportDto.getDeviceUniqueIdList());
            realTimeOverviewReportDto_today.setPlatformType(realTimeOverviewReportDto.getPlatformType());
            realTimeOverviewReportDto_today.setQueryStartTime(todayStart);
            realTimeOverviewReportDto_today.setQueryEndTime(todayEnd);
            realTimeOverviewReportDto_today.setSubDeviceUniqueIdList(realTimeOverviewReportDto.getSubDeviceUniqueIdList());
            RealTimeOverviewReportVo realTimeOverviewReportVo_today = realTimeOverviewQuery_expend(realTimeOverviewReportDto_today);
            realTimeOverviewResultVo.setToday(realTimeOverviewReportVo_today);

            //查询昨天的实时总览
            //昨天
            String yesterday = Tools.getYesterdayDateStr();
            //昨天开始时间
            Integer yesterdayStart = DateUtil.getStrTime(yesterday + " 00:00:00");
            //昨天的结束时间
            Integer yesterdayEnd = DateUtil.getStrTime(yesterday + " 23:59:59");
            //今天的参数对象
            RealTimeOverviewReportDto realTimeOverviewReportDto_yesterday = new RealTimeOverviewReportDto();
            BeanUtils.copyProperties(realTimeOverviewReportDto, realTimeOverviewReportDto_yesterday);
            realTimeOverviewReportDto_yesterday.setMerId(realTimeOverviewReportDto.getMerId());
            realTimeOverviewReportDto_yesterday.setDeviceType(realTimeOverviewReportDto.getDeviceType());
            realTimeOverviewReportDto_yesterday.setDeviceUniqueIdList(realTimeOverviewReportDto.getDeviceUniqueIdList());
            realTimeOverviewReportDto_yesterday.setPlatformType(realTimeOverviewReportDto.getPlatformType());
            realTimeOverviewReportDto_yesterday.setQueryStartTime(yesterdayStart);
            realTimeOverviewReportDto_yesterday.setQueryEndTime(yesterdayEnd);
            realTimeOverviewReportDto_yesterday.setSubDeviceUniqueIdList(realTimeOverviewReportDto.getSubDeviceUniqueIdList());
            RealTimeOverviewReportVo realTimeOverviewReportVo_yesterday = realTimeOverviewQuery_expend(realTimeOverviewReportDto_yesterday);
            realTimeOverviewResultVo.setYesterday(realTimeOverviewReportVo_yesterday);

            //查询本月的实时总览
            //获取本月第一天
            String month_start = DateUtil.getEndDayOrOneDay(new Date(),0);
            //本月第一天的开始时间戳
            Integer monthStart = DateUtil.getStrTime(month_start + " 00:00:00");
            //获取本月最后一天
            String month_end = DateUtil.getEndDayOrOneDay(new Date(),1);
            //本月最后一天的结束时间戳
            Integer monthEnd = DateUtil.getStrTime(month_end + " 23:59:59");
            //本月的参数对象
            RealTimeOverviewReportDto realTimeOverviewReportDto_month = new RealTimeOverviewReportDto();
            BeanUtils.copyProperties(realTimeOverviewReportDto, realTimeOverviewReportDto_month);
            realTimeOverviewReportDto_month.setMerId(realTimeOverviewReportDto.getMerId());
            realTimeOverviewReportDto_month.setDeviceType(realTimeOverviewReportDto.getDeviceType());
            realTimeOverviewReportDto_month.setDeviceUniqueIdList(realTimeOverviewReportDto.getDeviceUniqueIdList());
            realTimeOverviewReportDto_month.setPlatformType(realTimeOverviewReportDto.getPlatformType());
            realTimeOverviewReportDto_month.setQueryStartTime(monthStart);
            realTimeOverviewReportDto_month.setQueryEndTime(monthEnd);
            realTimeOverviewReportDto_month.setSubDeviceUniqueIdList(realTimeOverviewReportDto.getSubDeviceUniqueIdList());
            RealTimeOverviewReportVo realTimeOverviewReportVo_month = realTimeOverviewQuery_expend(realTimeOverviewReportDto_month);
            realTimeOverviewResultVo.setCurrentMonth(realTimeOverviewReportVo_month);
        }else{
            //只查询时间段内的数据
            RealTimeOverviewReportVo realTimeOverviewReportVo = realTimeOverviewQuery_expend(realTimeOverviewReportDto);
            realTimeOverviewResultVo.setToday(realTimeOverviewReportVo);
        }

        return realTimeOverviewResultVo;
    }


    //查询某个时间段内的实时总览数据
    private RealTimeOverviewReportVo realTimeOverviewQuery_expend(RealTimeOverviewReportDto realTimeOverviewReportDto){

        //查询缓存
        String redisKey = realTimeOverviewReportDto.signTopRequest();
        //查询缓存中是否报表
        Object object = redisTemplate.opsForValue().get(String.format(CacheKey.REPORT_REALTIMEOVERVIEW_EXPEND, redisKey));
        if(object != null){
            RealTimeOverviewReportVo realTimeOverviewReportVo = JSONObject.parseObject(object.toString(), RealTimeOverviewReportVo.class);
            return realTimeOverviewReportVo;
        }

        //是否页面选择了机器人-如果页面选择了机器人-则总预估佣金、总预估利润、有效订单笔数 取绑定绑定机器人数值
        boolean bandedDeviceFlag = false;

        RealTimeOverviewReportVo realTimeOverviewReportVo = new RealTimeOverviewReportVo();

        Long merId = realTimeOverviewReportDto.getMerId();
        String deviceType = realTimeOverviewReportDto.getDeviceType();
        //机器人查询条件
        List<Long> deviceUniqueIdList = realTimeOverviewReportDto.getDeviceUniqueIdList();
        //查询机器人不为空，则绑定了机器人
        if(deviceUniqueIdList != null && deviceUniqueIdList.size() > 0){
            bandedDeviceFlag = true;
        }

        //查询开始时间
        Integer queryStartTime = realTimeOverviewReportDto.getQueryStartTime();
        //查询结束时间
        Integer queryEndTime = realTimeOverviewReportDto.getQueryEndTime();
        //查询平台（默认查询全部）
        Integer platformType = Tools.getInteger(realTimeOverviewReportDto.getPlatformType());
        //判断查询条件
        if(merId == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "系统参数错误");
        }

        if(StringUtils.isBlank(deviceType)){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择设备类型");
        }

        if(queryStartTime == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入时间范围");
        }

        if(queryEndTime == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入时间范围");
        }

        int now = DateUtil.getNowTime();

        if(queryEndTime.intValue() < queryStartTime.intValue()){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "结束时间不能早于开始时间");
        }

        if(queryEndTime.intValue()- queryStartTime.intValue() > 31*24*3600){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "最多只能查询31天的报表");
        }

        if(now - queryStartTime.intValue() > 90*24*3600){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "最早只能查询90天以内的报表");
        }

        //新建返回到页面的LIST对象
        //封装微服务机器人参数
        List<Long> robotIds = new ArrayList<>();

        //个人微信
        if(DeviceTypeEnum.DT_PC_WECHAT.getKey().equals(deviceType)){
            DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
            BeanUtils.copyProperties(realTimeOverviewReportDto, deviceWechatInfoQuery);
            deviceWechatInfoQuery.setIdList(deviceUniqueIdList);
            deviceWechatInfoQuery.setMerId(merId);
            ArrayList<DeviceWechatInfo> deviceWechatInfoList =  deviceWechatInfoMapper.findByList(deviceWechatInfoQuery);
            if(deviceWechatInfoList != null && deviceWechatInfoList.size() > 0){
                for (DeviceWechatInfo deviceWechatInfo : deviceWechatInfoList){
                    robotIds.add(deviceWechatInfo.getId());
                }
            }
        }else if(DeviceTypeEnum.DT_WORK_WECHAT.getKey().equals(deviceType)){
            Page<DeviceWxworkInfo> page = new Page<>();
            DeviceWxworkInfoQuery deviceWxworkInfoQuery = new DeviceWxworkInfoQuery();
            BeanUtils.copyProperties(realTimeOverviewReportDto, deviceWxworkInfoQuery);
            deviceWxworkInfoQuery.setIdList(deviceUniqueIdList);
            deviceWxworkInfoQuery.setMerId(merId);
            ArrayList<DeviceWxworkInfo> deviceWxworkInfoList =  deviceWxworkInfoMapper.findByList(deviceWxworkInfoQuery);
            if(deviceWxworkInfoList != null && deviceWxworkInfoList.size() > 0){
                for (DeviceWxworkInfo deviceWxworkInfo : deviceWxworkInfoList){
                    robotIds.add(deviceWxworkInfo.getId());
                }
            }
        }else if(DeviceTypeEnum.WX_PUB.getKey().equals(deviceType)){
            WxpubInfoListQuery wxpubInfoListQuery = new WxpubInfoListQuery();
            BeanUtils.copyProperties(realTimeOverviewReportDto, wxpubInfoListQuery);
            wxpubInfoListQuery.setIdList(deviceUniqueIdList);
            wxpubInfoListQuery.setMerId(merId);
            List<WxpubInfoEntity> wxpubInfoEntityList =  wxpubInfoMapper.queryList(wxpubInfoListQuery);
            if(wxpubInfoEntityList != null && wxpubInfoEntityList.size() > 0){
                for (WxpubInfoEntity wxpubInfoEntity : wxpubInfoEntityList){
                    robotIds.add(wxpubInfoEntity.getId());
                }
            }
        }

        //有机器人才去查询
        if(robotIds != null && robotIds.size() > 0){

            //封装微服务机器人参数
            List<Long> robotIds_new = new ArrayList<>();
            for(Long deviceUniqueId : robotIds){
                //子账号负责的机器人列表
                List<Long> subDeviceUniqueIdList = realTimeOverviewReportDto.getSubDeviceUniqueIdList();
                if(subDeviceUniqueIdList != null && subDeviceUniqueIdList.size() > 0){
                    //子账号绑定了机器人
                    bandedDeviceFlag = true;
                    for (Long subDeviceUniqueId : subDeviceUniqueIdList){
                        if(subDeviceUniqueId.longValue() == deviceUniqueId.longValue()){
                            robotIds_new.add(deviceUniqueId);
                        }
                    }
                }else{
                    robotIds_new.add(deviceUniqueId);
                }
            }

            RealTimeOverviewReportFeignDto realTimeOverviewReportFeignDto = new RealTimeOverviewReportFeignDto();
            realTimeOverviewReportFeignDto.setStartTime(queryStartTime);
            realTimeOverviewReportFeignDto.setEndTime(queryEndTime);

            //判断是否绑定机器人
            if(bandedDeviceFlag){//绑定机器人才传送机器人列表参数
                realTimeOverviewReportFeignDto.setRobotIds(robotIds_new);
            }
            realTimeOverviewReportFeignDto.setPlatformType(platformType);
            realTimeOverviewReportFeignDto.setMerId(merId);
            realTimeOverviewReportFeignDto.setDeviceType(DeviceTypeEnum.getByKey(deviceType).getId());
            UzaiRespVo<RealTimeOverviewReportFeignVo> uzaiRespVo =  uzaiReportServerFeignService.realTimeOverviewSerach_expend(realTimeOverviewReportFeignDto);
            //logger.info("----调用实时总览指标微服务---para={}, result={}", JSONObject.toJSONString(realTimeOverviewReportFeignDto), JSONObject.toJSONString(uzaiRespVo));
            if(uzaiRespVo != null && uzaiRespVo.getCode() == 200){
                RealTimeOverviewReportFeignVo realTimeOverviewReportFeignVo = uzaiRespVo.getData();
                if(realTimeOverviewReportFeignVo != null){
                    //对象赋值
                    BeanUtils.copyProperties(realTimeOverviewReportFeignVo, realTimeOverviewReportVo);
                    //更新缓存
                    redisTemplate.opsForValue().set(String.format(CacheKey.REPORT_REALTIMEOVERVIEW_EXPEND, redisKey), Tools.getStrEmpty(JSONObject.toJSONString(realTimeOverviewReportVo)), 2, TimeUnit.MINUTES);
                }
            }
        }
        return realTimeOverviewReportVo;
    }

}
